import gameCtr from "../ctrs/gameCtr";
import { trackTarget } from "../libs/core/utils";
import BaseEle, { campType, soliderType } from "./baseEle";

/**
 * @Author: 陈瑞鹏
 * @Date:   2022-11-29 10:51:21
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-07 11:01:51
 */
const { ccclass, property } = cc._decorator;

// 重力
const G = -940;
// 固定速度
const V = 1000;

@ccclass
export default class fireBallEle extends cc.Component {

  @property({ type: cc.Sprite, tooltip: "贴图" })
  sp: cc.Sprite = null;

  //目标点
  public targetPos: cc.Vec2 = null;

  public startPos: cc.Vec2 = null;

  public aoeNum: number = 5;

  public masterType: soliderType = soliderType.cannon;

  private campType: campType;

  public setTargetPos(startPos, targetPos, aoeNum, type, campType) {
    this.targetPos = targetPos;
    this.startPos = startPos;
    this.aoeNum = aoeNum;
    this.masterType = type;
    this.campType = campType;
  }

  public isCollide() {
    return Math.sqrt(Math.pow(this.node.x - this.targetPos.x, 2) + Math.pow(this.node.y - this.targetPos.y, 2)) < 10;
  }

  public move(dt) {

    if (this.isCollide()) {
      // this.overHurt();
      this.recover();
    } else {
      this.trackTargetMove();
    }
  }

  /**
   * 面向目标移动
   */
  public trackTargetMove() {

    if (!this.targetPos) return;

    let speedArr = trackTarget(this.targetPos.x, this.targetPos.y, this.startPos.x, this.startPos.y, 10);

    this.node.x += speedArr[0];

    this.node.y += speedArr[1];

    this.node.angle = speedArr[2] * (180 / Math.PI);
  }

  /**
   * 范围伤害
   */
  public overHurt() {

    let childs = gameCtr.ins.eleNode.children;

    for (let i: number = 0; i < childs.length; i++) {

      let ele: cc.Node = childs[i];
      // console.log("hurtBlood");
      if (!ele) {
        // console.log("hurtBlood");
        continue;
      }

      if (ele.getComponent(BaseEle).type != soliderType.monster) continue;

      if (this.node.getBoundingBox().intersects(ele.getBoundingBox())) {

        ele.getComponent(BaseEle).hurtBlood(1);

      }
    }
  }

  public recover() {
    let wPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
    gameCtr.ins.curScene.emit("baozha", wPos, this.aoeNum);
    gameCtr.ins.poolObj['bullet'].put(this.node);
  }
}
